package cst.a327yjp.recyclertest327yjp.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a327yjp.recyclertest327yjp.R;
import cst.a327yjp.recyclertest327yjp.bean.ArticleBean;
import cst.a327yjp.recyclertest327yjp.adapter.MyAdapter;
import cst.a327yjp.recyclertest327yjp.iface.ArticleListener;
import cst.a327yjp.recyclertest327yjp.model.ArticleModel;

public class MainActivity extends AppCompatActivity implements ArticleListener{

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter myAdapter,mAdapter;
    List<ArticleBean> list;

    private void initRecylerView(){
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
         recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
       // list = getData();
        mAdapter = new MyAdapter(this);
        mAdapter.setList(list);
        recyclerView.setAdapter(mAdapter);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecylerView();
        ArticleModel articlemodel=new ArticleModel();
        articlemodel.getResultList("article",2,"2ts2j910hv4m270hipde4a67d7",this);
    }

//    private List<ArticleBean>getData(){
//        List<ArticleBean>list =new ArrayList<ArticleBean>();
//        ArticleBean art;
//
//        art =new ArticleBean();
//        art.setId(1);
//        art.setTitle("标题1");
//        art.setImgid(R.drawable.f1);
//        art.setTime("2017-5-12 16:53");
//        art.setDescription("描述1");
//        list.add(art);
//
//
//        return list;
//    }

    @Override
    public void onResponse(List<ArticleBean> list) {
        this.list=list;
        mAdapter.setList(list);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "fail:"+msg, Toast.LENGTH_SHORT).show();
    }
}
